package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.JsonResult;
import com.qf.pojo.Notice;
import com.qf.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @RequestMapping("findByPage")
    public JsonResult findByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "2") Integer pageSize
    ) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);

        // 查询所有通知
//        System.out.println("findAll");
        List<Notice> notices = noticeService.findAll();

        // 封装到pageInfo对象
        PageInfo<Notice> pageInfo = new PageInfo<>(notices);

        // 返回jsonResult对象
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setData(pageInfo.getList());
        jsonResult.setCount(pageInfo.getTotal());

        return jsonResult;
    }

    @RequestMapping("updateNotice")
    public String updateNotice(Notice notice){
        System.out.println("updateNotice");
        noticeService.updateNotice(notice);
        return "success";
    }

    @RequestMapping("deleteNotice")
    public String deleteNotice(Integer id){
        noticeService.deleteNotice(id);
        return "success";
    }

    @RequestMapping("deleteAll")
    public String deleteAll(Integer [] ids){
        noticeService.deleteAll(ids);
        return "success";
    }

    @RequestMapping("addNotice")
    public String addNotice(Notice notice){
        noticeService.addNotice(notice);
        return "success";
    }


}
